<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '活动添加' : '活动编辑'"
      :visible.sync="obj.isTrue"
      width="60%"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <div class="block">
          <span class="demonstration">活动期限</span>
          <el-date-picker
            class="box"
            v-model="value1"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <el-form-item label="一级分类">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="changeOne()"
          >
            <el-option label="请选择" :value="0" disabled></el-option>
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select
            v-model="form.second_cateid"
            placeholder="请选择活动区域"
            @change="changeTwo()"
          >
            <el-option label="请选择" :value="0" disabled></el-option>
            <el-option
              v-for="item in secondCate"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择商品">
          <el-select v-model="form.goodsid" placeholder="请选择三级分类">
            <el-option label="请选择" :value="0" disabled></el-option>
            <el-option
              v-for="item in secklisk"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="0"
            :inactive-value="1"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="enterEdit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  cateList,
  seckillAdd,
  seckillInfo,
  seckillEdit,
  goodsList,
} from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      value1: [],
      form: {
        title: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
        begintime: "",
        endtime: "",
      },
      secondCate: [],
      secklisk: [],
    };
  },
  computed: {
    ...mapGetters({
      seckillList: "seckill/list",
      cateList: "cate/list",
    }),
  },
  methods: {
    ...mapActions({
      asyncSeckillList: "seckill/asyncList",
      asyncCateList: "cate/asyncList",
      asyncGoodsList: "goods/asyncList",
    }),
    changeOne() {
      // 最终效果：在二级分类下面根据一级分类选中的东西，渲染二级分类
      // 调接口，用你刚才选中的那一项的id来请求他下面的子分类
      cateList({ pid: this.form.first_cateid }).then((res) => {
        // 给二级分类赋值
        this.secondCate = res.data.list;
        // 清空之前选中的二级分类
        this.form.second_cateid = 0;
      });
    },
    changeTwo() {
      goodsList({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        this.secklisk = res.data.list;
        this.form.goodsid = "";
      });
    },
    empty() {
      this.form = {
        title: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: 0,
        status: 1,
        begintime: "",
        endtime: "",
      };
      this.secondCate = [];
      this.secklisk = [];
      this.value1 = [];
    },
    cancel() {
      this.empty();
      this.obj.isTrue = false;
    },
    add() {
      this.form.begintime = new Date(this.value1[0]).getTime();
      this.form.endtime = new Date(this.value1[1]).getTime();
      seckillAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          //清空数据
          this.empty();
          //显示遮罩层
          this.obj.isTrue = false;
          //请求列表数据
          this.asyncSeckillList();
          //请求列表数据
          this.asyncGoodsList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getOne(id) {
      seckillInfo({ id: id }).then((res) => {
        // 1、给form赋值
        this.form = res.data.list;
        //获取时间戳渲染到页面
        this.value1.push(Number(res.data.list.begintime));
        this.value1.push(Number(res.data.list.endtime));
        // 3、根据刚刚获取到的一级分类，请求二级分类，渲染到页面中
        cateList({ pid: this.form.first_cateid }).then((res) => {
          // 给二级分类赋值
          this.secondCate = res.data.list;
        });
        goodsList({
          fid: this.form.first_cateid,
          sid: this.form.second_cateid,
        }).then((res) => {
          this.secklisk = res.data.list;
        });
        this.form.id = id;
      });
    },
    enterEdit() {
      seckillEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          //清空数据
          this.cancel();
          //请求列表数据
          this.asyncSeckillList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {
    this.asyncSeckillList();
    this.asyncCateList();
    this.asyncGoodsList();
  },
};
</script>
<style scoped>
.box {
  margin-left: 12px;
  margin-bottom: 15px;
}

.demonstration {
  padding-left: 10px;
}
</style>